package com.test.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;

import org.junit.Test;

public class ConStrMethodRef {

	@FunctionalInterface
	interface UserFactory<U extends User> {
		U create(int id, String name);

		default void say() {
			System.out.println("Hello My Name is liLi");
		}
	}

	static UserFactory<User> uf = User::new;

	public static void main(String[] args) {
		List<User> users = new ArrayList<User>();
		for (int i = 0; i < 10; i++) {
			users.add(uf.create(i, "billy " + Integer.toString(i)));
		}

		int[] arr = { 1, 2, 3, 4, 5, 6 };
		IntConsumer outPrintIn = System.out::print;
		IntConsumer errPrintIn = System.err::print;

		Arrays.stream(arr).forEach(outPrintIn.andThen(errPrintIn));
		users.stream().map(User::getName).forEach(System.out::println);
		double avg = users.parallelStream().mapToInt(s -> s.getId()).average().getAsDouble();
		System.out.println("avg=" + avg);
		long result = IntStream.range(0, 10000000).parallel().filter(PrimeUtil::isPrime).count();
		System.err.println("result=" + result);
	}

	public static class PrimeUtil {
		public static boolean isPrime(int number) {
			int tmp = number;
			if (tmp < 2) {
				return false;
			}
			for (int i = 2; i < Math.sqrt(tmp); i++) {
				if (tmp % i == 0) {
					return false;
				}
			}
			return true;
		}
	}

	@Test
	public void test() {
		int[] arr = { 5, 4, 9, 3, 56, 2, 5 };
		Arrays.parallelSort((arr));
		System.out.println(Arrays.toString(arr));
		Random r = new Random();
		Arrays.parallelSetAll(arr, (i) -> r.nextInt());
	}
}
